#include <iostream>
#include <fstream>
#include <time.h>
#include <memory.h>
#include <math.h>

#ifndef BM_MD2_H
#define BM_MD2_H
#ifdef __APPLE__
    #include <SDL/SDL.h>
#else
    #include <SDL/SDL.h>
    #include "GL/gl.h"
    #include "GL/glu.h"
#include <GL/glext.h>
    #include "SDL/SDL_image.h"
#endif

#define MD2_OK         0x1
#define MD2_ERR_MEM    0x2
#define MD2_ERR_FILE   0x3
#define MD2_ERR_FORMAT 0x4

/* Black Voltage team did not code this md2 loader. We have improved & modified it but we haven't coded it! We take no credits. */

struct MD2Header
{
    int ID;			// File Type - Normally 'IPD2'
    int Version;
    int TexWidth;   // Texture width
    int TexHeight;  // Texture height
    int FrameSize;  // Size for frames in bytes
    int nTextures;  // Number of textures
    int nVertices;  // Number of vertices
    int nTexCoords; // Number of UVs
    int nTriangles; // Number of polys
    int nGLCmd;     // Number of GL Commmands
    int nFrames;    // Number of frames
    int TexOffset;  // Offset to texture name(s)
    int UVOffset;   // Offset to UV data
    int FaceOffset; // Offset to poly data
    int FrameOffset;// Offset to first frame
    int GLCmdOffset;// Offset to GL Cmds
    int EOFOffset;  // Size of file
};

struct MD2FrameInfo
{
    float Scale[3];
    float Translate[3];
    char Name[16];
};


struct MD2Face
{
    short p1,p2,p3;
    short uv1,uv2,uv3;
};


struct MD2Vtx
{
    unsigned char Vtx[3];
    unsigned char lNorm;
};


struct Mesh_Vtx
{
    float x,y,z;
};


struct Mesh_UV
{
	 float u,v;
};


struct MD2Frame
{
    Mesh_Vtx *Vtx;
    Mesh_Vtx *Norm;
};


struct MD2TexCoord
{
    short u,v;
};

/*typedef struct MD2_Model
{
    float ViewRotate; // A few vars to handle view rotation, animation and time base values

    int Frames;
    int CurFrame;
    int NextFrame;

    GLuint Texture[32]; // Texture store

    long Time1,Time2,Ticks;

    int nFrames,nTri,nVtx,nUV;
    MD2Face *Face;
    MD2Frame *frame;
    Mesh_UV *UV;
    char TexName[64];
    GLuint TexID;

} MD2_Model;*/

class BMMD2
{
    private:
        float x, y, z;

    public:
        BMMD2();
        ~BMMD2();
        int loadMD2(char* filename);
        void initMD2();
        void drawMD2(int Frame);
        void moveMD2(float in_x, float in_y, float in_z);
        int GetFrameCount();
        char* GetTexName();
        void SetTexture(GLuint TexNum);
        void freeMD2();

        // model data
        float ViewRotate; // A few vars to handle view rotation, animation and time base values
        int Frames;
        int CurFrame;
        int NextFrame;
        GLuint Texture[32]; // Texture store
        long Time1,Time2,Ticks;
        int nFrames,nTri,nVtx,nUV;
        MD2Face *Face;
        MD2Frame *frame;
        Mesh_UV *UV;
        char TexName[64];
        GLuint TexID;
        float getX();
        float getY();
        float getZ();

    private:
        /*int nFrames,nTri,nVtx,nUV;
        MD2Face *Face;
        MD2Frame *frame;
        Mesh_UV *UV;
        char TexName[64];
        GLuint TexID;*/

        void CalcNormal(Mesh_Vtx v1,Mesh_Vtx v2,Mesh_Vtx v3,Mesh_Vtx* Result);
 };

#endif //END BM_OBJ_H
